Open
Conversation
spajic
approved these changes
Mar 14, 2025
| @@ -0,0 +1,53 @@ | |||
| # frozen_string_literal: true | |||
|
|
|||
| class TripsImporter | |||
| <% end %> | ||
| </ul> | ||
| <%= render "delimiter" %> | ||
| ==================================================== |
Collaborator
There was a problem hiding this comment.
Just for your info: https://guides.rubyonrails.org/layouts_and_rendering.html#spacer-templates
| Переписала TripsImporter с использованием Activerecord-Import, время загрузки изменилось | ||
| для файла `small.json` данные загружаются за 1.69s | ||
| для файла `medium.json` данные загружаются за 4.1s | ||
| для файла `large.json` данные загружаются за 13.8s |
| 22% времени тратится на запрос `SELECT "trips".* FROM "trips" WHERE "trips"."from_id" = $1 AND "trips"."to_id" = $2 ORDER BY "trips"."start_time" ASC` | ||
| Предлагается | ||
| `CREATE INDEX CONCURRENTLY ON trips (from_id, to_id)` | ||
| И 18% на `SELECT COUNT(*) FROM "trips" WHERE "trips"."from_id" = $1 AND "trips"."to_id" = $2` |
Collaborator
There was a problem hiding this comment.
count запрос вообще не нужен, так как мы грузим данные и можем просто взять size
| И 18% на `SELECT COUNT(*) FROM "trips" WHERE "trips"."from_id" = $1 AND "trips"."to_id" = $2` | ||
| Предлагается то же самое. | ||
| Добавляю индексы. | ||
| После добавления индексов процент снизился до 17% и 14% соответственно, скорость загрузки сократилась до 5837ms |
Collaborator
There was a problem hiding this comment.
скорость загрузки -> время загрузки (сорри за душноту 😁)
| rack-mini-profiler показывает что запросы на buses, buses_services и services выполняются отдельно. | ||
| Предполагаю что надо заменить в контроллере includes на eager_load, чтобы избавиться от этого. Теперь вместо 7 запросов - 4 и страница загружается за 1439ms | ||
|
|
||
| Менее 1,5 секунд уже приемлемое время, а что еще оптимизировать я с имеющимися инструментами не обнаружила. |
| После добавления индексов процент снизился до 17% и 14% соответственно, скорость загрузки сократилась до 5837ms | ||
|
|
||
| Так же pghero предлагает добавить индекс `CREATE INDEX CONCURRENTLY ON buses_services (bus_id)` | ||
| Хотя запрос `SELECT "buses_services".* FROM "buses_services" WHERE ....` занимает 3% |
Collaborator
There was a problem hiding this comment.
да, на время рендеренга нашей страницы это почти не повлияет; но если мы занимались оптимизацией именно нагрузки на БД, то там бы это имело смысл
| expect(bus.services.map(&:name)).to match_array(['WiFi', 'Туалет']) | ||
| end | ||
|
|
||
| it 'creates trips with correct attributes' do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.